package org.darkforest.admin.controller;

import org.darkforest.common.services.CountyService;
import org.darkforest.controller.WebController;
import org.darkforest.dao.mapper.CadreSupportPoorMapper;
import org.darkforest.dao.mapper.CountySupportPoorMapper;
import org.darkforest.model.Area;
import org.darkforest.model.CadreSupportPoor;
import org.darkforest.model.CountySupportPoor;
import org.darkforest.ucenter.service.AreaRedisService;
import org.darkforest.vo.WapAjaxMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by pc on 2018/5/2.
 */
@Controller
@RequestMapping(value = "setting")
public class CountyController extends WebController<CountyController> {
    @Autowired
    private CountySupportPoorMapper countyMapper;
    @Autowired
    private AreaRedisService areaRedisService;
    @Autowired
    private CadreSupportPoorMapper cadreMapper;
    @Autowired
    private CountyService countyService;

    @RequestMapping(value = "county")
    public ModelAndView list(HttpServletRequest request) {
        ModelAndView view = new ModelAndView("setting/county/list");
        CountySupportPoor county = new CountySupportPoor();
        county.setResultList(countyMapper.selectListByCondition(county));
        view.addObject("page", county);
        return view;
    }

    @RequestMapping(value = "county/areaList",method = RequestMethod.POST)
    public @ResponseBody
    WapAjaxMsg<List<Area>> getAddressAreaList(Integer paId){
//        Logger.debug(AddressController.class, "address" + paId);
        WapAjaxMsg<List<Area>> res = new WapAjaxMsg<List<Area>>();
        res.setDatas(areaRedisService.getAreaList(paId==null?null:paId.toString()));
        return res;
    }

    @RequestMapping(value = "county/toAdd")
    public ModelAndView toAdd() {
        ModelAndView view = new ModelAndView("setting/county/add");
        return view;
    }

    @RequestMapping(value = "county/doAdd")
    public ModelAndView doAdd(HttpServletRequest request,CountySupportPoor county) {
        ModelAndView modelAndView = new ModelAndView("msg");
        int i = countyMapper.insert(county);
        if (i > 0) {
            modelAndView.addObject("msg", "扶贫县新增成功");
        } else {
            modelAndView.addObject("msg", "扶贫县新增失败");
        }
        modelAndView.addObject("returnUrl", "setting/county");
        return modelAndView;
    }

    @RequestMapping(value = "county/toEdit")
    public ModelAndView toEdit(String countyId) {
        ModelAndView view = new ModelAndView("setting/county/edit");
        CountySupportPoor county = countyMapper.selectByPrimaryKey(Integer.valueOf(countyId));
        view.addObject("county", county);
        return view;
    }

    @RequestMapping(value = "county/doEdit")
    public ModelAndView doEdit(HttpServletRequest request,CountySupportPoor county) {
        ModelAndView modelAndView = new ModelAndView("msg");
        int i = countyMapper.updateByPrimaryKeySelective(county);
        if (i > 0) {
            modelAndView.addObject("msg", "扶贫县修改成功");
        } else {
            modelAndView.addObject("msg", "扶贫县修改失败");
        }
        modelAndView.addObject("returnUrl", "setting/county");
        return modelAndView;
    }

    /**
     * 扶贫县删除
     *
     * @param request
     * @return
     */
    @RequestMapping("county/delete")
    public ModelAndView delActivity(HttpServletRequest request) {
        String[] countyId = request.getParameterValues("countyId");
        int i = countyService.deleteCounty(countyId);
        ModelAndView modelAndView = new ModelAndView("msg");
        if (i == 1) {
            modelAndView.addObject("msg", "扶贫县删除成功");
        } else {
            modelAndView.addObject("msg", "扶贫县删除失败");
        }
        modelAndView.addObject("returnUrl", "setting/county");
        return modelAndView;
    }

    /**
     * 图片上传
     *
     * @param multipartRequeste
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping("county/ajaxFileUpload")
    public @ResponseBody String checkCountyName(MultipartHttpServletRequest multipartRequeste, HttpServletResponse response) throws IOException {

        Iterator<Map.Entry<String,MultipartFile>> it =  multipartRequeste.getFileMap().entrySet().iterator();
        String countyIcon = "";
        while(it.hasNext()){
            MultipartFile BrandPicturempf = it.next().getValue();
            countyIcon =  super.uploadPicsWithoutCompressSize(BrandPicturempf, multipartRequeste, null, null);
        }

        return countyIcon;

    }

    @RequestMapping(value = "county/cadre")
    public ModelAndView cadrelist(HttpServletRequest request,Integer countyId) {
        ModelAndView view = new ModelAndView("setting/county/cadre_list");
        CadreSupportPoor cadre = new CadreSupportPoor();
        cadre.setCountyId(countyId);
        cadre.setResultList(cadreMapper.selectListByCondition(cadre));
        view.addObject("page", cadre);
        return view;
    }

    @RequestMapping(value = "county/cadre/toAdd")
    public ModelAndView toAddCadre(Integer countyId) {
        ModelAndView view = new ModelAndView("setting/county/add_cadre");
        view.addObject("countyId", countyId);
        return view;
    }

    @RequestMapping(value = "county/cadre/doAdd")
    public ModelAndView doAddCadre(HttpServletRequest request,CadreSupportPoor cadre) {
        ModelAndView modelAndView = new ModelAndView("msg");
        String countyId = request.getParameter("countyId");
        cadre.setCountyId(Integer.valueOf(countyId));
        int i = cadreMapper.insertSelective(cadre);
        if (i > 0) {
            modelAndView.addObject("msg", "扶贫县干部新增成功");
        } else {
            modelAndView.addObject("msg", "扶贫县干部新增失败");
        }
        modelAndView.addObject("returnUrl", "setting/county/cadre?countyId="+countyId);
        return modelAndView;
    }

    @RequestMapping(value = "county/cadre/toEdit")
    public ModelAndView toEditCadre(String cadreId) {
        ModelAndView view = new ModelAndView("setting/county/edit_cadre");
        CadreSupportPoor cadre = cadreMapper.selectByPrimaryKey(Integer.valueOf(cadreId));
        view.addObject("cadre", cadre);
        return view;
    }

    @RequestMapping(value = "county/cadre/doEdit")
    public ModelAndView doEditCadre(HttpServletRequest request,CadreSupportPoor cadre) {
        ModelAndView modelAndView = new ModelAndView("msg");
        String countyId = request.getParameter("countyId");
        int i = cadreMapper.updateByPrimaryKeySelective(cadre);
        if (i > 0) {
            modelAndView.addObject("msg", "扶贫县干部修改成功");
        } else {
            modelAndView.addObject("msg", "扶贫县干部修改失败");
        }
        modelAndView.addObject("returnUrl", "setting/county/cadre?countyId="+countyId);
        return modelAndView;
    }

    /**
     * 扶贫县干部删除
     *
     * @param request
     * @return
     */
    @RequestMapping("county/cadre/delete")
    public ModelAndView delCadre(HttpServletRequest request) {
        String[] cadreId = request.getParameterValues("cadreId");
        CadreSupportPoor cadreSupportPoor = new CadreSupportPoor();
        String countyId = "";
        int i = 0;
        for (String id : cadreId) {
            cadreSupportPoor = cadreMapper.selectByPrimaryKey(Integer.valueOf(id));
            countyId = String.valueOf(cadreSupportPoor.getCountyId());
            i += cadreMapper.deleteByPrimaryKey(Integer.parseInt(id));
        }
        ModelAndView modelAndView = new ModelAndView("msg");
        if (i == cadreId.length) {
            modelAndView.addObject("msg", "扶贫县干部删除成功");
        } else {
            modelAndView.addObject("msg", "扶贫县干部删除失败");
        }
        modelAndView.addObject("returnUrl", "setting/county/cadre?countyId=" +countyId);
        return modelAndView;
    }
}
